package com.hjs.juc;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 创建线程的方式
 *  执行callable方式，需要FutureTask的支持，用于接收结果，也可用于闭锁
 * @author hanjinsong
 * @date 2020-02-19 14:25
 */
public class TestCallable {
    public static void main(String[] args) {
        CallableDemo callableDemo = new CallableDemo();
        FutureTask futureTask = new FutureTask<>(callableDemo);
        new Thread(futureTask).start();
        try {
            futureTask.get();//futureTask也可用于闭锁
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

class CallableDemo implements Callable {
    @Override
    public Object call() throws Exception {//有返回值，能抛出异常
        return null;
    }
}
